package com.zzh.myapplication.activity;

import android.app.AlertDialog;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Handler;
import android.os.Message;
import android.provider.ContactsContract;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;

import net.icarplus.car.R;
import net.icarplus.car.constant.UrlValues;
import net.icarplus.car.manager.BaseActivity;
import net.icarplus.car.service.PublicRequest;
import net.icarplus.car.tools.SPUtils;
import net.icarplus.car.tools.ToolBarHelper;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by Administrator on 2016/3/31.
 */
public class BaoCharteredActivity extends BaseActivity {
    public Toolbar toolbar;
    public TextView tv_bao_look_detail;
    private AlertDialog.Builder builder;
    private ImageView iv_close_dailodg;
    private ImageView rb_image;
    private ImageView rb_image1;
    private ImageView rb_image2;
    private ImageView iv_close_dailog_replace;
    private TextView tv_bao_reduce;
    private TextView tv_bao_index;
    private TextView tv_bao_add;
    private TextView tv_replace_car;
    private TextView tv_close_dailog;
    private EditText et_name;
    public int index = 1;
    public int index_add;
    public int index_reduce;
    private LinearLayout ll_economy_car;
    private LinearLayout ll_business_car;
    private LinearLayout ll_luxury_car;
    private LinearLayout ll_address_book;
    private LinearLayout ll_image;

    private LinearLayout ll_add_cad_bus;
    private ImageView iv_open_cad;
    private EditText et_add_phone;
    public Button btn_submit;
    private String carLevel = "01";

    public boolean isopen = false;

    @Override
    public void setContent() {
        setContentView(R.layout.activity_chartered_car);
    }

    @Override
    public void initComponent() {
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        ToolBarHelper.setNormalTitleAndBack(this, toolbar, "我要包车");
        tv_bao_look_detail = (TextView) findViewById(R.id.tv_bao_look_detail);
        tv_bao_reduce = (TextView) findViewById(R.id.tv_bao_reduce);
        tv_bao_index = (TextView) findViewById(R.id.tv_bao_index);
        tv_bao_add = (TextView) findViewById(R.id.tv_bao_add);
        tv_replace_car = (TextView) findViewById(R.id.tv_replace_car);
        ll_economy_car = (LinearLayout) findViewById(R.id.ll_economy_car);
        ll_business_car = (LinearLayout) findViewById(R.id.ll_business_car);
        ll_luxury_car = (LinearLayout) findViewById(R.id.ll_luxury_car);
        ll_add_cad_bus = (LinearLayout) findViewById(R.id.ll_add_cad_bus);
        ll_image = (LinearLayout) findViewById(R.id.ll_image);
        rb_image = (ImageView) findViewById(R.id.rb_image);
        rb_image1 = (ImageView) findViewById(R.id.rb_image1);
        rb_image2 = (ImageView) findViewById(R.id.rb_image2);
        iv_open_cad = (ImageView) findViewById(R.id.iv_open_cad);
        tv_bao_index.setText(index + "");
        btn_submit = (Button) findViewById(R.id.btn_submit);
    }

    public void GetSubmitCharteredBusParams() {
        JSONObject object = new JSONObject();
        try {
            object.put("mobile", SPUtils.getString(BaoCharteredActivity.this, "LoginInfo", ""));
            object.put("orderMode", "01");
            object.put("departType", "01");
            object.put("charterDay", index);
            object.put("departTime", "2016-04-13 17:16:01");
            object.put("departAddress", "");
            object.put("departAddressLongitude", "");
            object.put("departAddressLatitude", "");
            object.put("arriveAddress", "");
            object.put("arriveAddressLongitude", "");
            object.put("arriveAddressLatitude", "");
            object.put("carLevel", carLevel);
            object.put("replaceFlag", "1");
            object.put("reserveName", "");
            object.put("reservePhone", "");
            object.put("appointFlag", "");
            object.put("appointCustId", "11101");
            object.put("planAmt", "1");

        } catch (JSONException e) {
            e.printStackTrace();
        }
        PublicRequest.customMainRequest(this, UrlValues.GET_SUBMIT_CHARTEREDBUS, handler, object);
    }

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1000:
                    Gson gson = new Gson();
                    System.out.print("chantered:::" + msg.obj.toString());
                    break;
            }

        }
    };

    @Override
    public void setListener() {
        btn_submit.setOnClickListener(this);
        tv_bao_look_detail.setOnClickListener(this);
        tv_bao_reduce.setOnClickListener(this);
        tv_bao_index.setOnClickListener(this);
        tv_bao_add.setOnClickListener(this);
        ll_economy_car.setOnClickListener(this);
        ll_business_car.setOnClickListener(this);
        ll_luxury_car.setOnClickListener(this);
        tv_replace_car.setOnClickListener(this);
        iv_open_cad.setOnClickListener(this);
        ll_add_cad_bus.setOnClickListener(this);
    }

    @Override
    public void onAfterInit() {
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_bao_look_detail:
                builder = new AlertDialog.Builder(this);
                View view = View.inflate(this, R.layout.bao_dailog, null);
                builder.setView(view);
                final AlertDialog dialog = builder.show();
                iv_close_dailodg = (ImageView) view.findViewById(R.id.iv_close_dailodg);
                iv_close_dailodg.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dialog.dismiss();
                    }
                });
                break;
            case R.id.tv_replace_car:
                builder = new AlertDialog.Builder(this);
                View carview = View.inflate(this, R.layout.replace_car_dialog, null);
                builder.setView(carview);
                final AlertDialog _cardialog = builder.show();
                iv_close_dailog_replace = (ImageView) carview.findViewById(R.id.iv_close_dailog_replace);
                ll_address_book = (LinearLayout) carview.findViewById(R.id.ll_address_book);
                et_name = (EditText) carview.findViewById(R.id.et_name);
                et_add_phone = (EditText) carview.findViewById(R.id.et_add_phone);
                iv_close_dailog_replace.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        _cardialog.dismiss();
                    }
                });
                ll_address_book.setOnClickListener(this);
                break;
            case R.id.tv_bao_reduce:
                index_reduce = index - 1;
                if (index_reduce > 0) {
                    tv_bao_index.setText(index_reduce + "");
                }
                index = index_reduce;
                break;
            case R.id.tv_bao_add:
                index_add = index + 1;
                tv_bao_index.setText(index_add + "");
                index = index_add;
                break;
            case R.id.ll_economy_car:
                rb_image.setImageResource(R.mipmap.click_1);
                rb_image1.setImageResource(R.mipmap.click_2);
                rb_image2.setImageResource(R.mipmap.click_2);
                carLevel = "01";
                break;
            case R.id.ll_business_car:
                rb_image.setImageResource(R.mipmap.click_2);
                rb_image1.setImageResource(R.mipmap.click_1);
                rb_image2.setImageResource(R.mipmap.click_2);
                carLevel = "02";
                break;
            case R.id.ll_luxury_car:
                rb_image.setImageResource(R.mipmap.click_2);
                rb_image1.setImageResource(R.mipmap.click_2);
                rb_image2.setImageResource(R.mipmap.click_1);
                carLevel = "03";
                break;
            case R.id.ll_address_book:
                // Intent intent001 = new Intent();
                Intent intent = new Intent(Intent.ACTION_PICK,
                        ContactsContract.Contacts.CONTENT_URI);
                //intent001.setClassName("com.android.contacts", "com.android.contacts.activities.PeopleActivity");
                this.startActivityForResult(intent, 1);
                break;
            case R.id.iv_open_cad:
                if (isopen) {
                    iv_open_cad.setImageResource(R.mipmap.icon_check_yes);
                    ll_image.setVisibility(View.VISIBLE);
                    isopen = false;
                } else {
                    iv_open_cad.setImageResource(R.mipmap.icon_check_no);
                    ll_image.setVisibility(View.GONE);
                    isopen = true;
                }
                break;
            case R.id.btn_submit:
                GetSubmitCharteredBusParams();
                System.out.print("已执行");
                start_Activity(new Intent(BaoCharteredActivity.this, SubmitCharteredActivity.class));
                break;
            case R.id.ll_add_cad_bus:
                this.start_Activity(new Intent(BaoCharteredActivity.this, BaoAddCadActivity.class));
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {

            case (1): {

                if (resultCode == this.RESULT_OK) {

                    Uri contactData = data.getData();
                    Cursor c = managedQuery(contactData, null, null, null, null);
                    c.moveToFirst();
                    String phoneNum = this.getContactPhone(c);
                    if (phoneNum.contains("+86")) {
                        et_add_phone.setText(phoneNum.substring(3,phoneNum.length()));
                    } else {
                        et_add_phone.setText(phoneNum);
                    }
                    et_name.setText(name+"");
                }
                break;
            }
        }
    }

    int nameIndex;
    String name;
    //获取联系人电话
    private String getContactPhone(Cursor cursor) {

        int phoneColumn = cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER);
        int phoneNum = cursor.getInt(phoneColumn);
        String phoneResult = "";
        System.out.print(phoneNum + "----");
        if (phoneNum > 0) {
            // 获得联系人的ID号
            int idColumn = cursor.getColumnIndex(ContactsContract.Contacts._ID);
            nameIndex = cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
            String contactId = cursor.getString(idColumn);
            // 获得联系人的电话号码的cursor;
            Cursor phones = getContentResolver().query(
                    ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                    null,
                    ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactId,
                    null, null);
            //int phoneCount = phones.getCount();
            //allPhoneNum = new ArrayList<String>(phoneCount);
            if (phones.moveToFirst()) {
                System.out.print("111111" + "----");
                // 遍历所有的电话号码
                for (; !phones.isAfterLast(); phones.moveToNext()) {
                    int index = phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
                    int typeindex = phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE);
                    int phone_type = phones.getInt(typeindex);
                    name = cursor.getString(nameIndex);
                    String phoneNumber = phones.getString(index);
                    switch (phone_type) {
                        case 2:
                            phoneResult = phoneNumber;
                            break;
                    }
                    //allPhoneNum.add(phoneNumber);
                }
                if (!phones.isClosed()) {
                    phones.close();
                }
            }
        }
        return phoneResult;
    }

    @Override
    protected void onRestart() {
        super.onRestart();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }
}
